#include "SDLGame.h"
#include "SDLKeyboard.h"
#include "SDLVisualObject.h"
#include "KeyboardPlayerInput.h"
#include "AccurateTimer.h"
#include "SDLVisualObjectProvider.h"
#include <string>
#include <iostream>

SDLGame::SDLGame(void):
	Game()
{


	initSDL();
	keyboard = new SDLKeyboard();
	visualObjectProvider = new SDLVisualObjectProvider(screen);
	
	buildSplashScreen();
	buildPlayfield();
	buildPlayers();
	buildBall();
	buildScoreCounter();
	buildPauseImage();


}

void SDLGame::initSDL()
{
	if(SDL_Init(SDL_INIT_EVERYTHING))
	{
		throw std::string("Cannot initialise SDL. Make sure you're not running from console?\n");
	}

	// Set the Program icon...
	SDL_WM_SetIcon(SDL_LoadBMP("images/icon.bmp"), NULL);
	screen = SDL_SetVideoMode(width, height, bpp, SDL_SWSURFACE);

	if(screen == 0)
	{
		throw std::string("Cannot SetVideoMode.\n");
	}

	SDL_WM_SetCaption("PongPong 0.2", "PongPong");

}

void SDLGame::buildSplashScreen()
{
	splashscreen = new SplashScreen(new SDLVisualObject(screen));
}
void SDLGame::buildPauseImage()
{
	pauseimage = new PauseImage(*playfield, new SDLVisualObject(screen));
}

void SDLGame::buildPlayers()
{
	KeyboardPlayerInput* rightKbpi = new KeyboardPlayerInput(keyboard, KEY_DOWN, KEY_UP);
	KeyboardPlayerInput* leftKbpi = new KeyboardPlayerInput(keyboard, KEY_d, KEY_e);

	rightPlayer = new Player(PP_RIGHT, rightKbpi, new SDLVisualObject(screen), *playfield, *gamemode);
	leftPlayer = new Player(PP_LEFT, leftKbpi, new SDLVisualObject(screen), *playfield, *gamemode);


}

void SDLGame::buildBall()
{
	ball = new Ball(visualObjectProvider, 
		*playfield, 
		*leftPlayer, 
		*rightPlayer,
		*gamemode);

}

void SDLGame::buildPlayfield()
{
	playfield = new Playfield(width, height, new SDLVisualObject(screen));

}

void SDLGame::buildScoreCounter()
{
	scorecounter = new ScoreCounter(*playfield);
}
void SDLGame::gameLoop()
{
	bool quit = false;
	SDL_Event event;

	AccurateTimer timer;

	while(!quit)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				quit = true;
			}
			
			dynamic_cast<SDLKeyboard*>(keyboard)->newEvent(event);
		}

		drawObjects();
		double timePassed = timer.reset();
		update(timePassed);

		if( SDL_Flip(screen) == -1)
			throw std::string("Cannot flip screen. ");
		
		SDL_Delay(1);

	}
}

SDLGame::~SDLGame(void)
{
}

void SDLGame::drawObjects()
{
	if(*gamemode == GM_PLAY || *gamemode == GM_INTRO || *gamemode == GM_PAUZED)
	{
		leftPlayer->getReadyForDraw();
		rightPlayer->getReadyForDraw();
		ball->getReadyForDraw();
		

		playfield->draw();
		leftPlayer->draw();
		rightPlayer->draw();
		ball->draw();

		//only draw the pause image if the game is paused
		if(*gamemode == GM_PAUZED)
		{
			pauseimage->getReadyForDraw();
 			pauseimage->draw();
		}
	}

	if(*gamemode == GM_SPLASH)
	{
		splashscreen->getReadyForDraw();
		splashscreen->draw();

	}
}

